/**
 * Created by Eoly on 2017/2/16.
 * Call http://palerock.cn
 */
var billPage = angular.module('billPage', ['ngRoute']);

//自定义过滤器,负数为- 0或整数为+
billPage.filter('PlusMinus',function(){
    return function(input){
        //input 就是我们要处理的数据
        return input<0?'-':'+';
    }
});

/**
 * 用户账户
 */
billPage.controller('bill', function ($scope, $http) {
    $scope.suiMobileInit = function (isNotChangeContent) {
        app.suiInit(isNotChangeContent);

    };

    //声明账单数组
    $scope.bills = [];

    //获取用户账单
    $scope.getUserBills = function () {

        var token = app.getUserToken();

        if(!token){
            return;
        }

        if (token) {
            $http({
                url: net + '/user/show-bills',
                method: 'get',
                headers: {'Authorization': 'Bearer ' + token}
            }).then(function (res) {
                console.log(res);
                if (!res.data.error) {
                    $scope.bills = res.data.dataList;
                } else {
                    $.alert('错误代码：' + res.data.error + ' ' + res.data.mes);
                }
            }, function () {
                $.alert('网络或设置错误');
            })
        }
    };

    $scope.userBalance = 0;

    // 获取用户余额
    $scope.checkBalance = function () {
        var token = app.getUserToken();

        if(!token){
            return;
        }

        if (token) {
            $http({
                url:net + '/user/show-balance',
                headers: {'Authorization': 'Bearer ' + token}
            }).then(function (res) {
                console.log(res);
                if (!res.data.error) {
                    $scope.userBalance = res.data.data.userBalance;
                } else {
                    alert('错误代码：' + res.data.error + ' ' + res.data.mes);
                }
            }, function () {
                alert('网络或设置错误');
            })
        }
    };

    //声明密码类型以及对应id
    $scope.pswTypes=[];
    $scope.pswTypes.push({
        pswTypeName:'选择密码类型'
    },{
        pswTypeName:'登录密码',
        pswTypeId:'0'
    },{
        pswTypeName:'支付密码',
        pswTypeId:'1'
    });

    //声明修改密码需要的参数对象
    $scope.paramForModifyPsw = {};

    //修改用户密码
    $scope.modifyPsw = function(){

        var token = app.getUserToken();

        if(!token){
            return;
        }

        if (token) {
            $http({
                url: net + '/user/modify-psw',
                method: 'post',
                params: $scope.paramForModifyPsw,
                headers: {'Authorization': 'Bearer ' + token}
            }).then(function (res) {
                console.log(res);
                if (!res.data.error) {
                    $.alert(res.data.mes);
                    $scope.paramForModifyPsw = {};
                } else {
                    $.alert('错误代码：' + res.data.error + ' ' + res.data.mes);
                }
            }, function () {
                $.alert('网络或设置错误');
            })
        }
    };

});

/**
 * 路由
 */
billPage.config(function ($routeProvider) {
    $routeProvider.when('/all', {
        templateUrl: 'html/bill-all-content.html',
        controller: 'bill'
    }).when('/show', {
        templateUrl: 'html/bill-show-content.html',
        controller: 'bill'
    }).when('/psw', {
        templateUrl: 'html/bill-psw-content.html',
        controller: 'bill'
    }).otherwise({
        redirectTo: '/all'
    });
});